﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using UCS.XNA.Utils;
using UCS.XNA.Utils.Geometries;

namespace DivideAndConquer.States.InGame
{
    public class WallSegment : IRectangle
    {
        #region Constants

        private const float THICKNESS = 7f;
        private const float GROW_SPEED = 2f;

        #endregion

        #region Private Members

        private Texture2D _growingTexture;
        private Texture2D _normalTexture;
        private WallGrowDirection _growDirection;

        #endregion

        #region Attributes and Properties

        private float _width;
        public float Width
        {
            get { return _width; }
        }

        private float _height;
        public float Height
        {
            get { return _height; }
        }

        private float _scale = 1f;
        public float Scale
        {
            get { return _scale; }
            set { _scale = value; }
        }

        private float _x;
        public float X
        {
            get { return _x; }
        }

        private float _y;
        public float Y
        {
            get { return _y; }
        }

        private bool _keepGrowing = true;
        public bool KeepGrowing
        {
            get { return _keepGrowing; }
            set { _keepGrowing = value; }
        }

        private Vector2 _direction;
        public Vector2 Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }

        private float _speedFactor;
        public float SpeedFactor
        {
            get { return _speedFactor; }
            set { _speedFactor = value; }
        }

        private Vector2 _position;
        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        #endregion

        #region Constructors

        public WallSegment(Vector2 position, WallGrowDirection growDirection)
        {
            _position = position;

            _x = position.X;
            _y = position.Y;

            if (growDirection == WallGrowDirection.Left || growDirection == WallGrowDirection.Right)
            {
                _width = 0f;
                _height = THICKNESS;
                _y -= _height / 2;
            }
            else
            {
                _width = THICKNESS;
                _height = 0f;
                _x -= _width / 2;
            }

            _growDirection = growDirection;

            this.InitializeTextures();
        }

        #endregion

        #region Private Methods

        private void InitializeTextures()
        {
            _growingTexture = Texture2DCreator.FromColor(Color.Orange, GameResources.Graphics.GraphicsDevice);
            _normalTexture = Texture2DCreator.FromColor(Color.Orange, GameResources.Graphics.GraphicsDevice);
        }

        #endregion

        #region Public Methods

        public void Update(GameTime gameTime)
        {
            if (!_keepGrowing)
                return;

            switch (_growDirection)
            {
                case WallGrowDirection.Top:
                    _y -= GROW_SPEED;
                    _height += GROW_SPEED;
                    break;
                case WallGrowDirection.Right:
                    _width += GROW_SPEED;
                    break;
                case WallGrowDirection.Bottom:
                    _height += GROW_SPEED;
                    break;
                case WallGrowDirection.Left:
                    _x -= GROW_SPEED;
                    _width += GROW_SPEED;
                    break;
            }
        }

        public void Draw(GameTime gameTime)
        {
            GameResources.SpriteBatch.Begin();

            GameResources.SpriteBatch.Draw(texture: _keepGrowing ? _growingTexture : _normalTexture,
                destinationRectangle: new Rectangle((int)_x, (int)_y, (int)_width, (int)_height),
                sourceRectangle: null,
                color: Color.White,
                rotation: 0,
                origin: new Vector2(),
                effects: SpriteEffects.None,
                layerDepth: 0);

            GameResources.SpriteBatch.End();
        }

        #endregion
    }
}